package com.hitgo.soccer.core;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import com.hitgo.soccer.config.MatchConfig;
import com.hitgo.soccer.config.PlayerConfig;
import com.hitgo.soccer.config.PositionConfig;
import com.hitgo.soccer.utils.CalcUtil;

public class Team  implements Serializable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= -9118256565420214028L;

	private long 	userID = 0;
	private String 	teamName;
	private int		teamLevel;
	private int		vMoney;
	private	int		rMoney;
	private int 	energy;
	private	int		honor;
	private	int		logo;
	private	int		packCount;

	private Player curPlayer = null;

	private Player goalkeeper = null;
	private Player[] allPlayers;

	public void setPlayersPosition(boolean isL2R)
	{
		if (allPlayers == null)
			return;
		if (isL2R) // 左向右进攻
		{
			this.goalkeeper.setX(this.goalkeeper.getInitX());
			this.goalkeeper.setY(this.goalkeeper.getInitY());
			for (Player p : allPlayers)
			{
				p.setX(p.getInitX());
				p.setY(p.getInitY());
			}
		}
		else
		{
			this.goalkeeper.setX((short) (MatchConfig.fieldWidth - this.goalkeeper.getInitX()));
			this.goalkeeper.setY((short) (MatchConfig.fieldHeight - this.goalkeeper.getInitY()));
			for (Player p : allPlayers)
			{
				p.setX((short) (MatchConfig.fieldWidth - p.getInitX()));
				p.setY((short) (MatchConfig.fieldHeight - p.getInitY()));
			}
		}
	}

	public Player getCurPlayer()
	{
		return curPlayer;
	}

	public Player setCurPlayer(Player p)
	{
		return curPlayer = p;
	}

	public Player getNearestPlayer(int x, int y)
	{
		double nD = 0;
		Player player = null;
		for (Player p : this.allPlayers)
		{
			double d = CalcUtil.getDistance(p.getX(), p.getY(), x, y);
			if (player == null)
			{
				player = p;
				nD = d;
			}
			else if (d < nD)
			{
				player = p;
				nD = d;
			}
		}
		return player;
	}

	public Player getDefindPlayer(short x, short y)
	{
		double nD = 0;
		Player player = null;
		for (Player p : this.allPlayers)
		{
			if(p.isRange(x, y) == false)
				continue;
			double d = CalcUtil.getDistance(p.getX(), p.getY(), x, y);
			if (player == null)
			{
				player = p;
				nD = d;
			}
			else if (d < nD)
			{
				player = p;
				nD = d;
			}
		}

		// 查找所有的人员看看是否在防守范围呢
		if (player != null && player.isPause())
			return null;
		
		return player;
	}

	public Player getGoalkeeper()
	{
		return goalkeeper;
	}

	public void setGoalkeeper(Player goalkeeper)
	{
		this.goalkeeper = goalkeeper;
	}

	public long getUserID()
	{
		return userID;
	}

	public void setUserID(long userID)
	{
		this.userID = userID;
	}

	public int getFrontPlayerNum(boolean isL2R)
	{
		if(this.curPlayer == this.goalkeeper)
			return allPlayers.length;
		
		ArrayList<Player> alP = getFrontPlayers(isL2R);
		
		return alP.size();
	}
	
	public ArrayList<Player> getFrontPlayers(boolean isL2R)
	{
		boolean isPassBack = false;
		if(isL2R)
		{
			if(curPlayer.getX() == MatchConfig.fieldWidth)
				isPassBack = true;
		}
		else
		{
			if(curPlayer.getX() == 0)
				isPassBack = true;
		}
		if(isPassBack)
			isL2R = !isL2R;
		
		ArrayList<Player> alPlayers = new ArrayList<Player>();
		int nIndex = 0;
		for(nIndex = 0; nIndex < allPlayers.length; nIndex++)
		{
			Player p = allPlayers[nIndex];
			if(p == this.curPlayer)
				continue;
			if(isL2R)
			{
				if(p.getX() <= curPlayer.getX())
					continue;
					
			}
			else
			{
				if(p.getX() >= curPlayer.getX())
					continue;
			}
			if(alPlayers.size() == 0)
				alPlayers.add(p);
			else
			{
				Player p2 = null;
				for(int nAI = 0; nAI < alPlayers.size(); nAI++)
				{
					p2 = alPlayers.get(nAI);
					if(isL2R)
					{
						if(p.getX() < p2.getX())
						{
							alPlayers.set(nAI, p);
							p = p2;
						}
					}
					else
					{
						if(p.getX() > p2.getX())
						{
							alPlayers.set(nAI, p);
							p = p2;
						}
					}
				}
				alPlayers.add(p);
			}
		}
		return alPlayers;
	}
	
	public void setPlayers(Player[] players)
	{
		this.allPlayers = new Player[6];
		for(Player p:players)
		{
			PlayerConfig plConfig = PlayerConfig.getPlayerConfig(p.getPlayerID());
			if(plConfig == null)
			{
				System.out.println("" + p.getPlayerID());
			}
			p.setPlayerData(plConfig);
			
			PositionConfig psConfig = PositionConfig.getPosition(p.getPosition());
			p.setPosData(psConfig);
			
			if(p.getPosition() == PositionConfig.GOALKEEPER)
			{
				this.goalkeeper = p;
			}
			else
			{
				//	排序
				for(int nIndex = 0; nIndex < allPlayers.length; nIndex++)
				{
					if(allPlayers[nIndex] == null)
					{
						allPlayers[nIndex] = p;
						break;
					}
					else
					{
						if(allPlayers[nIndex].getX() > p.getX())
						{
							Player pTemp = allPlayers[nIndex];
							allPlayers[nIndex] = p;
							p = pTemp;
						}
					}
					
				}
			}
		}
	}
	
	public Player[] getAllPlayers()
	{
		return this.allPlayers;
	}
	
	public byte[] toByte()
	{
		// TODO 需要序列化
		ByteBuffer byteData  = ByteBuffer.allocate(1024*1024);
		
		return byteData.array();
	}
	
	public void allPlayersMove(boolean isL2R)
	{
		for(Player p:this.allPlayers)
		{
			if(p == this.getCurPlayer() || p.getPosition() == PositionConfig.GOALKEEPER)
				continue;
			p.move(isL2R);
		}
	}

	public int getvMoney()
	{
		return vMoney;
	}

	public void setvMoney(int vMoney)
	{
		this.vMoney = vMoney;
	}
	
	public int addvMoney(int value)
	{
		this.vMoney -= value;
		return this.vMoney;
	}
	
	public int getrMoney()
	{
		return rMoney;
	}

	public void setrMoney(int rMoney)
	{
		this.rMoney = rMoney;
	}
	
	public int addrMoney(int value)
	{
		this.rMoney -= value;
		return this.rMoney;
	}
	public int getPackCount()
	{
		return packCount;
	}

	public void setPackCount(int pack_count)
	{
		this.packCount = pack_count;
	}
}
